package com.hr.bigData.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hr.approval.entity.ApprovalData;
import com.hr.approval.mapper.ApprovalDataMapper;
import com.hr.approval.service.ApprovalDataService;
import com.hr.bigData.entity.ApprovalDataEchartsVO;
import com.hr.bigData.entity.BigData;
import com.hr.classes.entity.TbWorkRecord;
import com.hr.classes.entity.dto.TbWorkRecordDTO;
import com.hr.classes.entity.dto.TbWorkRecordPerDto;
import com.hr.classes.mapper.TbWorkRecordMapper;
import com.hr.common.retrun.R;
import com.hr.system.entity.SysUser;
import com.hr.system.entity.vo.LoginUser;
import com.hr.system.mapper.SysDeptMapper;
import com.hr.system.mapper.SysUserMapper;
import com.hr.system.service.SysDeptService;
import com.hr.system.util.DateUtil;
import io.swagger.annotations.Api;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/bigData")
@Api(tags = "大屏展示")
@CrossOrigin
public class BigDataController {

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper SysUserMapper;
    @Autowired
    private TbWorkRecordMapper tbWorkRecordMapper;
    @Autowired
    private ApprovalDataMapper approvalDataMapper;
    @Resource
    private ApprovalDataService approvalDataService;
    @GetMapping("/info")
    public R getBigDataInfo(SysUser sysUser) throws ParseException {
        BigData res =new BigData();
        res.setUnitNum(sysDeptMapper.selectList(null).size());
        res.setEmpNum(SysUserMapper.selectUserList(new SysUser().setType(1)).size());
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        res.setTodayClock(tbWorkRecordMapper.selectList(Wrappers.<TbWorkRecord>lambdaQuery()
                .eq(TbWorkRecord::getDate, dateFormat.format(date))).size());
        List<ApprovalData> approvalDataList = approvalDataMapper.selectList(Wrappers.<ApprovalData>lambdaQuery()
                .ge(ApprovalData::getCreateTime, LocalDateTime.now().with(LocalTime.MIDNIGHT))
        );
        res.setApprovalNum(approvalDataList.size());
        res.setApprovalBu(approvalDataList.stream().filter(x->x.getType()==1).count());
        res.setApprovalShi(approvalDataList.stream().filter(x->x.getType()==2).count());
        res.setApprovalYear(approvalDataList.stream().filter(x->x.getType()==3).count());
        res.setApprovalDataEchartsVO(approvalDataService.getApprovalDataEcharts());
        Date[] dates = DateUtil.calMonthDate();
        TbWorkRecordPerDto tbWorkRecordPerDto = TbWorkRecordPerDto.builder().userId(sysUser.getId()).beginDate(dates[0]).endDate(dates[1]).build();
        List<TbWorkRecord> recordByDate = tbWorkRecordMapper.getRecordByDate(tbWorkRecordPerDto);
        List<Date> okList = new ArrayList<>();
        List<Date> errorList = new ArrayList<>();
        if (recordByDate.size()>0){
            for (TbWorkRecord tbWorkRecord : recordByDate) {
                if (!tbWorkRecord.getNormal().isEmpty()) {
                    if (tbWorkRecord.getNormal().equals("正常")) {
                        okList.add(tbWorkRecord.getDate());
                    } else if (tbWorkRecord.getNormal().equals("异常")) {
                        errorList.add(tbWorkRecord.getDate());
                    }
                }
            }
        }

        res.setOkClock(okList);
        res.setErrorClock(errorList);
        return R.ok(res);
    }

}
